/*
 * Copyright 2009, Qys Creations 
 * 
 *  This file is part of Chameleon DB.
 *
 *  Chameleon DB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chameleon DB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chameleon DB.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */


import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class db_parse_result_create_database implements db_interface_parse_result{
	public String sDBName = "";
	private int iRes = 0;

	public int get_result() {
		return iRes;
	}
	public void set_result(int iResult) {
		iRes = iResult;
	}
	
	public db_interface_result run_query(db_server oServer, db_session oSession){
		
		String sResult = "";
		try {

			RandomAccessFile oFile = null;
			try {
				File oNewFile = new File(db_util.DB_DATABASE_FOLDER + sDBName + db_util.DB_DATABASE_FILENAME_EXT);
				File oNewLog = new File(db_util.DB_DATABASE_FOLDER + sDBName + db_util.DB_DATABASE_LOGNAME_EXT);
				if (!oNewFile.exists() && !oNewLog.exists())  {
					if (oNewFile.createNewFile() && oNewLog.createNewFile()) {
					oNewFile.createNewFile();
					oFile = new RandomAccessFile(db_util.DB_DATABASE_FOLDER + sDBName + db_util.DB_DATABASE_FILENAME_EXT, "rwd");
					
					db_book.newBook(new db_file(oFile.getChannel()));
					db_book oBook = new db_book(oFile);
					
					System.out.println("ready!");
				
					db_reading_glass oGlass = new db_reading_glass(oBook, oFile.getChannel());
					if (oGlass.groupCount() == 0) {
						oGlass.createGroup();
						oGlass.createGroup();
					}
					oGlass.begin();
					oGlass.seekGroup(0);
					oGlass.writeStr(db_util.DB_VERSION_IDENTIFIER, db_util.DB_BYTE_LENGTH);
					oGlass.writeStr(sDBName, db_util.DB_BYTE_LENGTH);
					
					oGlass.seekGroup(1);
					oGlass.writeInt(0);
					oGlass.commit();
					oServer.add_database(new db_database(db_util.DB_DATABASE_FOLDER + sDBName + db_util.DB_DATABASE_FILENAME_EXT));
				
					sResult = "The database " + sDBName + " has been created succesfully";
					
					
					}
					else
						sResult = "Could not create the database.";
				}
				else
					sResult = "There is already a database by the name '" + sDBName + "' on the server.";
			} catch (Exception e) {e.printStackTrace();}
		} catch (Exception e) {
			e.printStackTrace();
			sResult = "A file system error prevented the creation of the database";
		}

		
		//oServer.add_database(new db_database(db_util.DB_DATABASE_FOLDER + sDBName + ".qdb"));
		return new db_result_message(db_util.DB_MESSAGE_OK, sResult);
	} 
}
